package com.lujianfei.powerinput.ui.noteList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.lujianfei.powerinput.R;
import com.lujianfei.powerinput.base.BaseActivity;
import com.lujianfei.powerinput.base.CommonAdapter;
import com.lujianfei.powerinput.base.ViewHolder;
import com.lujianfei.powerinput.database.NoteItemFieldModel;
import com.lujianfei.powerinput.database.NoteItemModel;
import com.lujianfei.powerinput.ui.noteList.base.NoteListViewContract;
import com.lujianfei.powerinput.ui.noteList.presenter.NoteListEditPresenter;
import com.lujianfei.powerinput.ui.noteList.presenter.NoteListViewPresenter;

import java.util.ArrayList;
import java.util.List;

public class NoteListViewActivity extends BaseActivity implements NoteListViewContract.View {

    private NoteItemModel noteItemModel;
    private NoteListViewContract.Presenter presenter;
    private ListView listview;
    private CommonAdapter<NoteItemFieldModel> commonAdapter;
    private final List<NoteItemFieldModel> noteItemFieldModelList = new ArrayList<>();
    private ProgressBar progress_bar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_list_view);
        initView();
        presenter = new NoteListViewPresenter(this);
        parseIntent();
        if (noteItemModel!=null) {
            setTitleMiddleText(noteItemModel.getNoteName()+"信息");
        }else{
            setTitleMiddleText("信息");
        }
        presenter.init();
    }

    private void initView() {
        listview = findViewById(R.id.listview);
        progress_bar = findViewById(R.id.progress_bar);
    }

    private void parseIntent() {
        Intent intent = getIntent();
        noteItemModel = intent.getParcelableExtra("data");
        presenter.setData(noteItemModel);
    }

    @Override
    public Activity currentActivity() {
        return this;
    }

    @Override
    public void updateList(List<NoteItemFieldModel> noteItemFieldModels) {
        if (null == commonAdapter){
            commonAdapter = new CommonAdapter<NoteItemFieldModel>(this,noteItemFieldModelList,R.layout.adapter_note_view_item) {
                @Override
                public void convert(ViewHolder viewHolder, NoteItemFieldModel data) {
                    viewHolder.setText(R.id.tv_title,data.getKey()+":");
                    viewHolder.setText(R.id.tv_content,data.getValue());
                }
            };
            listview.setAdapter(commonAdapter);
        }
        this.noteItemFieldModelList.clear();
        this.noteItemFieldModelList.addAll(noteItemFieldModels);
        commonAdapter.notifyDataSetChanged();
        progress_bar.setVisibility(View.GONE);
    }

    @Override
    protected int getTitleBarResId() {
        return R.id.custom_titlebar;
    }

    @Override
    protected void onDestroy() {
        presenter.onDestroy();
        super.onDestroy();
    }
}
